package environment;

import jade.core.Agent;

import java.util.Random;

public class Tablero {
	
	private int dim;
	private int size;
	private int activePackages;
	private Casilla[][] casillas;
	private int usedEnergy;
	
	/***/

	public Tablero(int dim, int packages) {
		if (dim < 10) {
			this.dim = 10;
			this.size = 100;
		} else {
			this.dim = dim;
			this.size = dim*dim;
		}
		casillas = new Casilla[this.dim][this.dim];
		for (int i = 0; i < dim; i++){
			for (int j = 0; j < dim; j++){
				casillas[i][j] = new Casilla(this);
			}
		}
		
		this.usedEnergy = 0;
		
		if (packages < 4)
			this.activePackages = 4;
		else if (packages > (this.size/2)) 
			this.activePackages = this.size/2;
		else
			this.activePackages = packages;
		
		setLoader();
		setPortals();
		setPackages();
	}

	/***/

	public int getDim() {
		return dim;
	}
	
	/***/

	public int getSize() {
		return size;
	}
	
	/***/

	public int getActivePackages() {
		return activePackages;
	}
	
	/***/

	public Casilla[][] getCasillas() {
		return casillas;
	}
	
	/***/
	
	public static int randInt(int min, int max) {
	    Random rand = new Random();
	    int randomNum = rand.nextInt((max - min) + 1) + min;
	    return randomNum;
	}
	
	/***/

	private void setLoader() {
		System.out.println("Setting loader");
		int fil = randInt(0,this.dim - 1);
		int col = randInt(0,this.dim - 1);
		casillas[fil][col].setContenido(1);
		for (int i=0; i < this.casillas.length; i++) {
			for (int j=0; j < this.casillas.length; j++) {
				casillas[i][j].setGradiente(Math.max(Math.abs(fil - i), Math.abs(col - j)));
			}
		}
		
	}
	
	/***/

	private void setPortals() {
		System.out.println("Setting portals");
		int fil;
		int col;
		for (int p = 2; p < 6; p++) {
			do {
				fil = randInt(0,this.dim-1);
				col = randInt(0,this.dim-1);
			} while (casillas[fil][col].getContenido() != 0);
			casillas[fil][col].setContenido(p);
		}
		
	}
	
	/***/

	private void setPackages() {
		System.out.println("Setting packages");
		int fil;
		int col;
		for (int i = 0; i < activePackages; i++) {
			do {
				fil = randInt(0,this.dim-1);
				col = randInt(0,this.dim-1);
			} while (casillas[fil][col].getContenido() != 0);
			casillas[fil][col].setContenido((i%4)+6);
		}
		
	}
	
	/***/
	
	public int[] getCoordenadas(Casilla casilla) {
		int [] coordenadas = new int[2];
		for (int i=0; i < this.dim; i++) {
			for (int j=0; j < this.dim; j++) {
				if (this.casillas[i][j] == casilla) {
					coordenadas[0] = i;
					coordenadas[1] = j;
				}
			}
		}
		return coordenadas;
	}
	
	/***/

	public Casilla requestStartPosition(Agent a) {
		int fil;
		int col;
		
		do {
			fil = randInt(0, this.dim - 1);
			col = randInt(0, this.dim - 1);
		} while (casillas[fil][col].getContenido() != 0);
		casillas[fil][col].setContenido(10);
		casillas[fil][col].updateGui();
		return casillas[fil][col];
	}
	
	/***/
	
	public Casilla[][] getVision(Casilla center) {
		Casilla[][] vision = new Casilla[5][5];
		int[] centCoord = getCoordenadas(center);
		int ix, ij;
		
		for(int i = -2; i < 3; i++) {
			for(int j = -2; j < 3; j++) {
				if (centCoord[0] + i >= 0 && centCoord[0] + i < this.dim && centCoord[1] + j >= 0 && centCoord[1] + j < this.dim) {
					ix = (centCoord[0]+i);
					ij = (centCoord[1]+j);
					//System.out.println("Vision: (" + ix + "," + ij +")");
					vision[i+2][j+2] = casillas[centCoord[0]+i][centCoord[1]+j];	
				} else {
					ix = (centCoord[0]+i);
					ij = (centCoord[1]+j);
					//System.out.println("No vision: (" + ix + "," + ij +")");
					vision[i+2][j+2] = null;
				}
			}
        } 
		
		return vision;
	}

	/***/
	
	public void onePacketLess() {
		this.activePackages--;
	}

	/***/
	
	public void addUsedEnergy(int uE) {
		this.usedEnergy = this.usedEnergy + uE;
	}

	/***/
	
	public int getUsedEnergy() {
		return this.usedEnergy;
	}
}
